<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var str = "    abc  def  ghi jkl  mn  "
        //循环去空格
        var stt = "";
        for (var i = 0; i < str.length; i++) {
            if (str[i] != " ") {
                stt += str[i];
            }
        }
        str = stt;
        console.log(str);
        //正则表达式去空格
        var str = "    abc  def  ghi jkl  mn  ";
        str = str.replace(/\s+/g, "");
        console.log(str);

        //正则案例 练习 查看编辑器如何使用正则
        console.log("moon2xyz".replace(/o/,"ab"));//mabon2xyz
        console.log("moon2xyz".replace(/o/g,"ab"));//mababn2xyz
        console.log("moon2 ooxyz".replace(/\bo/g,"ab"));//moon2 aboxyz
        console.log("moon2xyz".replace(/\dx/,"_"));//moon_yz
        console.log("moon2xyz".replace(/[xyz]/g,"ab"));//moon2ababab
        console.log("m2on2x2z".replace(/\d[zo]/g,"ab"));//mabn2xab
        console.log("m2on2x2z".replace(/2[x-z]/g,""));//m2on        

        //将下列文章中 单独的大写C（前后没有字符）统一改为大写D，要求其他的c不受影响
        
        var str = "Chaude and Cold A patron in Montreal cafe turned on a tap in the washroom and got scalded. This is an outrage, he complained. The faucet marked C gave me boiling water.But, Monsieur, C stands for chaude - French for hot. You should know that if you live in Montreal.Wait a minute, roared the patron. The other tap is also marked C.Of course, said the manager, It stands for cold. After all, Montreal is a bilingual city."
        var reg = /C(?=\W)/mg;
        var newStr = str.replace(reg,"D");
        console.log(newStr);

        // 正则测试工具
        // http://www.iteye.com/news/29859
        // https://rubular.com/r/xfQHocREGj

        //常见正则案例
        // https://blog.csdn.net/liangqibinde/article/details/79205585

        // 参考文档
        // https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Regular_Expressions
    
        //new运算符
        var box1 = new RegExp('box'); //第一个参数字符串
        var box2 = new RegExp('box', 'ig'); //第二个参数可选模式修饰符
        //字面量创建
        var box3 = /box/; //直接用两个反斜杠
        var box4 = /box/ig; //在第二个斜杠后面加上模式修饰符

        console.log(box1); //  /box
        console.log(box2); //  /box/gi
        console.log(box3); //  /box
        console.log(box4); //  /box/gi


        //正则对象的创建方式一 通过字面量直接创建
        var reg1 = /[bcf]at/gi;

        //正则对象的创建方式二 通过RegExp构造函数来实例化正则对象
        var reg2 = new RegExp(/[bcf]at/,"gi");//常见形式
        var reg3 = new RegExp("[bcf]at","gi");

        console.log("a fAt bat ,a faT cat".match(reg1));//["fAt", "bat", "faT", "cat"]
        console.log("a fAt bat ,a faT cat".match(reg2));//["fAt", "bat", "faT", "cat"]
        console.log("a fAt bat ,a faT cat".match(reg3));//["fAt", "bat", "faT", "cat"]

        var reg4 = new RegExp(/[bc]at/,"g"); //全部匹配
        "a fAt Bat ,a faT cat".replace(reg4,"yy");
        //'a fAt Bat ,a faT yy'
        var reg5 = new RegExp(/[bc]at/,"gi"); //全部匹配+忽略大小写
        "a fAt Bat ,a faT cat".replace(reg5,"yy");
        //'a fAt yy ,a faT yy'
        var reg6 = new RegExp(/[bc]at/,"i"); //忽略大小写
        "a fAt Bat ,a faT cat".replace(reg6,"yy");
        //'a fAt yy ,a faT cat'


        // 转义字符:
        // 双（单）引号里面的第一个 \ 识别成转义字符,解析之后不会在浏览器输出
        //匹配\
        var pattern1 = /\\/;
        var pattern2 = new RegExp('\\\\'); //先解析为字符串 再解析为正则
        console.log(pattern1); //  /\\/
        console.log(pattern2); //  /\\/
    
        //字符串
        var str1 = "How are you?\nI'm fine!";
        var str2 = "How are you?\\nI'm fine!";
        var str3 = "How are you?\\\\nI'm fine!";
        console.log(str1);
        console.log(str2);
        console.log(str3);

    </script>
</body>
</html>